package com.example.bbsbe.controller;

import com.example.bbsbe.entity.comments;
import com.example.bbsbe.entity.profile;
import com.example.bbsbe.service.CommentsService;
import com.example.bbsbe.service.ProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class CommentsController {
    private final CommentsService commentsService;

    @Autowired
    public CommentsController(CommentsService commentsService) {
        this.commentsService = commentsService;
    }
    @GetMapping("/api/comments/")
    public ResponseEntity<?> getcomments() {
        List<comments> response = commentsService.findAll();
        return ResponseEntity.ok(response);
    }
    @PostMapping("/sentcom/")
    public ResponseEntity<?> createComment(@RequestBody Map<String, Object> request) {
        String proId = (String) request.get("pro_id");
        String content = (String) request.get("maintext");
        String username = (String) request.get("username");

        Map<String, Object> response = commentsService.createComment(proId, content, username);
        return ResponseEntity.ok(response);
    }
    @PostMapping("/getcom/")
    public ResponseEntity<?> getUserComments(@RequestBody Map<String, String> request) {
        String username = request.get("username");
        List<Map<String, Object>> response = commentsService.getUserComments(username);
        return ResponseEntity.ok(response);
    }
    @PostMapping("/deletecom/")
    public ResponseEntity<?> deleteComment(@RequestBody Map<String, Long> request) {
        Long comId = request.get("comid");
        Map<String, Object> response = commentsService.deleteComment(comId);
        return ResponseEntity.ok(response);
    }

}
